﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OAuth2.Framework;
using OAParameters = OAuth2.Framework.Parameters;
using OAuth2.Framework.Utility;

namespace OAuth2.Framework.Model
{
    public class AccessTokenModel : IToken
    {
        public AccessTokenModel() {
            IssuedOn = DateTime.Now.ToEpoch();
            Parameters = new Dictionary<string, string>();
        }

        #region IToken 成员

        public virtual string Token { get; set; }

        public virtual string TokenType { get; set; }

        public virtual int ExpiresIn { get; set; }

        public virtual string RefreshToken { get; set; }

        public virtual string[] Scope { get; set; }

        public virtual IDictionary<string, string> Parameters { get; private set; }

        public virtual long IssuedOn { get; set; }

        public virtual IDictionary<string, object> ToResponseVaules() 
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();

            dictionary.Add(OAParameters.AccessToken, Token);
            dictionary.Add(OAParameters.AccessTokenExpiresIn, ExpiresIn);
            dictionary.Add(OAParameters.RefreshToken, RefreshToken);
            dictionary.Add(OAParameters.AccessTokenType, TokenType);

            if (Scope != null && Scope.Length > 0)
                dictionary.Add(OAParameters.Scope, string.Join(" ", Scope));

            foreach (var item in Parameters)
            {
                dictionary.Add(item.Key, item.Value);
            }

            return dictionary;
        }

        public virtual AuthorizationGrantModel Grant { get; set; }

        public virtual IClient Client { get; set; }

        #endregion


    }
}
